package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConst;
import com.itheima.constant.RedisConst;
import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import com.itheima.utils.ObjectTranscoderUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.List;

/**
 * @author 黑马程序员
 * @Company http://www.ithiema.com
 * @Version 1.0
 */
@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {

    @Reference
    SetmealService setmealService;
    @Autowired
    JedisPool jedisPool;

    @RequestMapping("/getSetmeal")
    public Result getSetmeal(){
        log.debug("SetmealController： getSetmeal");
            //判断是否有数据
        Jedis jedis = jedisPool.getResource();
        if (jedis.exists(RedisConst.Set_Meal_Type.getBytes())){
            //有则取出 转换成集合
            byte[] bytes = jedis.get(RedisConst.Set_Meal_Type.getBytes());
            List<Setmeal> setmealList = (List<Setmeal>) ObjectTranscoderUtils.deserialize(bytes);
            log.debug("setmealList=========="+setmealList);
            jedis.close();
            return new Result(true, MessageConst.QUERY_SETMEALLIST_SUCCESS, setmealList);
        }
        List<Setmeal> setmealList = setmealService.findAll();
        jedis.set(RedisConst.Set_Meal_Type.getBytes(), ObjectTranscoderUtils.serialize(setmealList));
        log.debug("查询所有套餐成功！！");
        log.debug(setmealList.toString());
        jedis.close();
        return new Result(true, MessageConst.QUERY_SETMEALLIST_SUCCESS, setmealList);
    }

    @RequestMapping("/findDetailById")
    public Result findDetailById(Integer id){
        log.debug("SetmealController:findDetailById: " + id);
        Jedis jedis = jedisPool.getResource();
        if (jedis.exists(RedisConst.Set_Meal_content.getBytes())){
            //有则取出 转换成集合
            byte[] bytes = jedis.get(RedisConst.Set_Meal_content.getBytes());
            Setmeal  setmeal = (Setmeal) ObjectTranscoderUtils.deserialize(bytes);
            log.debug("setmeal=========="+setmeal);
            jedis.close();
            return new Result(true, MessageConst.QUERY_SETMEALLIST_SUCCESS, setmeal);
        }
        Setmeal setmeal = setmealService.findDetailById(id);
        jedis.set(RedisConst.Set_Meal_content.getBytes(), ObjectTranscoderUtils.serialize(setmeal));
        jedis.close();
        log.debug("查询套餐详情成功");
        log.debug(setmeal.toString());
        return new Result(true,MessageConst.QUERY_SETMEAL_SUCCESS,setmeal);
    }

    @RequestMapping("/findById")
    public Result findById(Integer id){
        log.debug("SetmealController:findById: " + id);
        Setmeal setmeal = setmealService.findById(id);
        log.debug("根据id查询套餐成功");
        return new Result(true,MessageConst.QUERY_SETMEAL_SUCCESS,setmeal);
    }



}
